<template>
  <div>
    <sticky-top>
      <div class="title">
        <span>{{ isCreate ? '创建房间' : '更新房间' }}</span>
        <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
        <el-divider></el-divider>
      </div>
    </sticky-top>
    <div class="container">
      <div class="wrap">
        <el-row>
          <el-col :lg="16" :md="20" :sm="24" :xs="24">
            <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
              <el-form-item label="房间名称" prop="name">
                <el-input size="medium" v-model="form.name" placeholder="请填写房间名称"></el-input>
              </el-form-item>
              <el-form-item label="房间管理员" prop="manager">
                <el-input size="medium" v-model="form.manager" placeholder="请填写房间管理员"></el-input>
              </el-form-item>
              <el-form-item label="联系方式" prop="phone">
                <el-input size="medium" v-model="form.phone" placeholder="请填写联系方式"></el-input>
              </el-form-item>

              <el-form-item label="所在楼栋" prop="category_id">
                <el-autocomplete
                  @focus="loadBuildingSuggestions"
                  popper-class="my-autocomplete"
                  class="inline-input"
                  v-model="form.belong_building"
                  :fetch-suggestions="queryCategorySearch"
                  placeholder="请选择所在楼栋"
                  @select="handleCategorySelect"
                >
                  <template slot-scope="{ item }">
                    <span class="id">{{ item.id }}</span>
                    <span class="name">{{ item.name }}</span>
                  </template>
                </el-autocomplete>
              </el-form-item>

              <el-form-item label="所在楼层" prop="floor">
                <el-input size="medium" v-model="form.floor" placeholder="请填写房间所在楼层"></el-input>
              </el-form-item>


<!--              <el-form-item label="描述" prop="description">-->
<!--                <el-input size="medium" v-model="form.description" placeholder="请填写描述"></el-input>-->
<!--              </el-form-item>-->

              <el-form-item class="submit">
                <el-button
                  v-permission="{ permission: ['创建房间', '更新房间'], type: 'disabled' }"
                  type="primary"
                  @click="submitForm('form')"
                >保 存</el-button
                >
                <el-button @click="resetForm('form')">重 置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import Building from '@/model/ag_building'
import Room from '@/model/ag_room'

export default {
  props: {
    isCreate: {
      type: Boolean,
      default: false,
    },
    roomId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      form: {
        building_id:'',
        belong_building:'',
        name: '',
        manager: '',
        phone: '',
        floor:'',
      },
      rules: {
        minWidth: 100,
        minHeight: 100,
        maxSize: 5,
      },
      // buildingName: '',
      buildings: [],
      specKeySuggestions: [],
      skuSuggestions: [],
    }
  },
  async mounted() {
    if (!this.isCreate) {
      const res = await Room.getRoom(this.roomId)
      this.form = res
      this.initData = [
        {
          id: res.id,
        },
      ]
      // this.buildingName=res.building_id
    }
  },
  methods: {
    // eslint-disable-next-line
    async submitForm(formName) {
      // await this.getValue()
      const postData = { ...this.form }
      // postData.spec_key_id_list = this.specs.map(spec => spec[0])
      let res
      if (this.isCreate) {
        res = await Room.addRoom(postData)
      } else {
        res = await Room.editRoom(this.roomId, postData)
      }
      if (res.code < window.MAX_SUCCESS_CODE) {
        this.$message.success(`${res.message}`)
        // this.resetForm(formName)
        this.$confirm('是否返回上一页', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(async () => {
          this.$emit('editClose')
        })
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async loadBuildingSuggestions() {
      if (this.buildings.length > 0) {
        return
      }
      try {
        this.buildings = await Building.getBuildings()
        if (this.buildings.length === 0) {
          this.$message.error('暂无楼栋信息，请先添加')
        }
      } catch (error) {
        this.$message.error('获取楼栋信息失败')
        console.error(error)
      }
    },
    queryCategorySearch(queryString, cb) {
      // eslint-disable-next-line
      const suggestions = this.buildings
      const results = queryString ? suggestions.filter(this.createCategoryFilter(queryString)) : suggestions
      cb(results)
    },
    createCategoryFilter(queryString) {
      // eslint-disable-next-line
      return suggestion => {
        return suggestion.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0
      }
    },
    handleCategorySelect(item) {
      this.form.belong_building = item.name
      this.form.building_id=item.id
    },

    back() {
      this.$emit('editClose')
    },


  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}

.my-autocomplete {
  li {
    line-height: normal;
    padding: 7px;
    display: inline-flex;
    align-content: space-around;
    .name {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .id {
      margin-right: 15px;
      font-size: 12px;
      color: #b4b4b4;
    }

    .highlighted .addr {
      color: #ddd;
    }
  }
}
</style>
